package com.dcy.daily.common.dataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 多线程的情况下保障线程隔离，维护一个ThreadLocal变量，来保证多线程下线程隔离。
 * @author duanchunyu
 * @version 1.0.0
 * @createTime 2021年06月28日 09:26:00
 */
public class DataSourceManager {

    public static final Logger logger = LoggerFactory.getLogger(DataSourceManager.class);


    private static final ThreadLocal<String> DATASOURCE_KEY = new ThreadLocal();


    public static void setDataSourceKey(String dataSourceKey){
        logger.info("切换数据源到：{}" + dataSourceKey);
        DATASOURCE_KEY.set(dataSourceKey);
    }

    public static String getDataSourceKey(){
        return DATASOURCE_KEY.get();
    }

    public static void clearDataSourceKey(){
        DATASOURCE_KEY.remove();
    }

}
